<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>
<body>
    <form class="layui-form">
        课程名:
        <div class="layui-inline">
            <input type="text" name="name" value="" class="layui-input" lay-affix="clear">
        </div>
        学分:
        <div class="layui-inline">
            <input type="text" name="credit" value="" class="layui-input" lay-affix="clear">
        </div>
        <div class="layui-inline">
            <button class="layui-btn" lay-submit lay-filter="submitSearch">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
    <table class="layui-hide" id="tableId" lay-filter="tableId"></table>
    <script src="/static/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="/static/mylayer.js" type="text/javascript" charset="utf-8"></script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteAll">批量删除</button>
        </div>
    </script>
    <script>
        /*layui.use(['table', 'form'], function () {
            var table = layui.table;
            var form = layui.form;
        })*/
        layui.use(['table','form'], function(){
            var table = layui.table;
            var form = layui.form;

            // 创建渲染实例
            table.render({
                elem: '#tableId',
                url:'/course?method=selectByPage', // 此处为静态模拟数据，实际使用时需换成真实接口
                toolbar: '#toolbarDemo',
                page: true,
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field:'id', fixed: 'left', title: 'ID', sort: true},
                    {field:'name', title: '课程名'},
                    {field:'credit', title: '学分'},
                    {fixed: 'right', title:'操作', width: 125, minWidth: 125, toolbar: '#barDemo'}
                ]]
            });

            // 搜索提交
            form.on('submit(submitSearch)', function(data){
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('tableId', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: field // 搜索的字段
                    // http://localhost:8080/course?method=selectByPage&page=1&limit=10&name=zhangsan&credit=12
                });

                return false; // 阻止默认 form 跳转
            });

            // 工具栏事件
            table.on('toolbar(tableId)', function(obj){
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch(obj.event){
                    case 'deleteAll':
                        var data = checkStatus.data;
                        //[{id: 1, name: 'java', credit: 5},{id: 2, name: 'UI', credit: 4}]
                        console.log(data);
                        var ids = new Array();
                        $(data).each(function () {
                            ids.push(this.id);
                        })
                        //[2,4]
                        layer.confirm('真的删除行么', function(index){
                            $.post(
                                '/course?method=deleteAll',
                                {'ids': ids},
                                function (result) {
                                    //{code: 0, msg: '删除成功', data: null}
                                    console.log(result);
                                    if (result.code == 0) {
                                        mylayer.okMsg(result.msg);
                                        //删除成功之后刷新表格，展示最新数据
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(result.msg);
                                    }
                                },
                                'json'
                            );
                        });
                        break;
                    case 'add':
                        layer.open({
                            type: 2,
                            title: '添加数据',
                            area: ['90%', '40%'],
                            content: '/course_add.jsp'
                        })
                        break;
                };
            });

            //触发单元格工具事件
            table.on('tool(tableId)', function(obj){ // 双击 toolDouble
                var data = obj.data;
                //console.log(obj)
                //{id: 1, name: 'java', credit: 5}
                console.log(data);
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.post(
                            '/course?method=deleteById',
                            {'id': data.id},
                            function (result) {
                                //{code: 0, msg: '删除成功', data: null}
                                console.log(result);
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    //删除成功之后刷新表格，展示最新数据
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'
                        );
                    });
                } else if(obj.event === 'edit'){
                    layer.open({
                        type: 2,
                        title: '修改数据',
                        area: ['90%', '40%'],
                        content: '/course_update.jsp?id=' + data.id
                    })
                }
            });

        });
    </script>
</body>
</html>
